<?php
/*
 * Created on 2012-9-6
 * Creater :cnluckylee
 * Creater mail:cnlucklee@gmail.com
 */
  class Vid_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 		$this->load->library('Tools');
 	}
 	public function Show($sday,$eday,$search_keyword,$caption_name)
 	{
	 	$int_sdate = strtotime($sday);

		$int_edate = strtotime($eday." +1 day");

		$where = " where ts.wid=".$this->session->userdata('wid')."  and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate ." and tv.".$search_keyword."!=''";
		$sql = "SELECT tv.".$search_keyword.",sum(ts.stopTime) as stopTimes,count(ts.arriveCount) as total_arrive,sum(ts.isbad) ".
				" as badclicks,sum(if(ts.firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,ts.vid from tj_vistor as tv".
				" LEFT JOIN tj_stat as ts on tv.vid=ts.vid".
					$where .
				" GROUP BY tv.".$search_keyword.",ts.vid ";

		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();

		foreach($arr as $kk=>$vv)
		{
			$key = $vv[$search_keyword];
			@$data[$key]['stopTimes'] += $vv['stopTimes'];
			@$data[$key]['total_arrive'] += $vv['total_arrive'];
			@$data[$key]['vid'][]= $vv['vid'];
			@$data[$key]['badclicks'] +=$vv['badclicks'];
			@$data[$key]['isnew'] +=$vv['isnew'];
		}
		if($data)
		{
			foreach($data as $k=>$v)
			{
				$data[$k]['vidCount'] = count(array_unique($v['vid']));
				$data[$k]['avg_arriveCount'] = rand($v['total_arrive']/$data[$k]['vidCount'],2);
				$data[$k]['avg_stopTime'] = rand($v['stopTimes']/$data[$k]['vidCount'],2);
			}
		}
		$xml = $this->make_xml($data,$caption_name,'total_arrive');
		$result_arr = array();

		$result_arr['data'] = $data;
		$result_arr['xml'] = $xml;

		return $result_arr;
 	}

	function OnLine()
	{
		$wid = $this->session->userdata('wid');
		$vid_arr = $this->redisinit->query('wid_vid_'.$wid.'_*');
		$arr = $result = array();
		foreach($vid_arr as $res)
		{
			$arr[] = $res['vid'];
		}
		$sql_vid = implode(',',$arr);
		if($sql_vid)
		{
			$sql = 'select * from tj_vistor where wid='.$wid.' and vid in ('.$sql_vid.') group by vid order by id desc ';
			$query = $this->db->query($sql);
			$arr = $query->result_array();
		}
		foreach($arr as $k=>$v)
		{
			$vid_arr = $this->redisinit->get('vid_'.$v['vid']);
			$v['arriveTimes'] =$vid_arr['arriveTimes'];
			$result[] = $v;
		}
		return $result;
	}

 	function make_xml($data,$caption_name,$value_name)
 	{


		$str = '<?xml version="1.0" encoding="gb2312"?>';
		$str .= '<chart caption="'.$caption_name.'" baseFontSize="14" palette="2" animation="1" formatNumberScale="0" pieSliceDepth="30" startingAngle="125" showValues="1" showPercentInToolTip="1" showLabels="1" showLegend="1" formatNumber="1">';
		foreach($data as $k=>$v)
		{
			$str .= '<set label="'.$k.'" value="'.$v[$value_name].'" isSliced="1"/>';
		}
		$str .='</chart>';
		return $str;
	}
  }
?>
